import axios from 'axios';
import store from '../store/index';

import { Message } from 'element-ui';
import router from '../router/index';
const request = axios.create({
  // 开发环境使用相对路径，通过代理转发请求
  baseURL: 'http://120.26.233.80:8084',
  timeout: 60000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8'
  }
});


// 请求拦截器
request.interceptors.request.use(config => {
  if (config.url.includes('/recruiter/auth/applyRegisterCompany') || config.url.includes('/recruiter/auth/getVerifyMessage')) {
    const tempToken = store.state.recruiter.tempToken
    if (tempToken) {
      config.headers.Authorization = `Bearer ${tempToken}`;

    }
    return config
  }
  // 尝试从不同用户类型获取token
  const seekerToken = store.state.seeker.token;
  const recruiterToken = store.state.recruiter.token;
  const managerToken = store.state.manager.token;

  // 根据URL前缀选择对应的token
  if (config.url.startsWith('/seeker')) {
    if (seekerToken) {
      config.headers.Authorization = `Bearer ${seekerToken}`;
    }
  } else if (config.url.startsWith('/recruiter')) {
    if (recruiterToken) {
      config.headers.Authorization = `Bearer ${recruiterToken}`;
    }
  } else if (config.url.startsWith('/manager')) {
    if (managerToken) {
      config.headers.Authorization = `Bearer ${managerToken}`;

    }
  }
  return config;
});


export default request;